<?php
namespace app\admin\controller;

use think\Request;

class Auth extends Common
{
    public function index(){
        $getRoles = model('role');
        $roles = $getRoles->getRoles();
        $this->assign('roles',$roles);
        return view();
    }
    //角色添加
    public function add(){
        $getPermission = model('Permission');
        $rolePermission = model('RolePermission');
        $hasPermission_ids = $getPermission->getPermission();
        $permission_list = $getPermission->getPermission();
        foreach ($permission_list as $k1 => $v1) {
            foreach ($permission_list as $k2 => $v2) {
                if($v2['pid']!=0 && $v1['permission_id']==$v2['pid']){
                    $permission_list[$k1]['son'][] = $v2;
                    unset($permission_list[$k2]);
                }
            }
        }
        //dump($permission_list);die;
        $this->assign('permission_list',$permission_list);
        return view();
    }


    //角色入库
    public function add_role(){
        $name =input('post.name');
        $permission_ids = input('post.')['permission_id'];
        $data =['name'=>$name];
        $validate = validate('Rolename');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }
        $ret = db('role')->insert($data);
        if($ret){
            $ret1 = db('role')->where("name = '$name'")->find();
            $role_id = $ret1['role_id'];
            $rolePermission = model('RolePermission');
            $rolePermission->addPermission($role_id,$permission_ids);
            $this->success('添加角色成功!','Auth/index');
        }else{
            $this->error('添加失败');
        }
        
    }
    //角色编辑
    public function edit(){
        $role_id = input('get.role_id'); 
        $role = db('role')->where("role_id = '$role_id'")->find();
        $getPermission = model('Permission');
        $rolePermission = model('RolePermission');
        $permission_ids = $rolePermission->getPermissionIds($role_id);
        $hasPermission_ids = $getPermission->getPermission($permission_ids);
        // dump($hasPermission_ids);die;

        foreach ($hasPermission_ids as $key => $value) {
            $hasPermission_ids[$key] = $value['permission_id'];
        }
        $permission_list = $getPermission->getPermission();
        foreach ($permission_list as $k1 => $v1) {
            foreach ($permission_list as $k2 => $v2) {
                if($v2['pid']!=0 && $v1['permission_id']==$v2['pid']){
                    $permission_list[$k1]['son'][] = $v2;
                    unset($permission_list[$k2]);
                }
            }
        }
        $this->assign('role',$role);
        //dump($role);die;
        $this->assign('hasPermission_ids',$hasPermission_ids);
        $this->assign('permission_list',$permission_list);
        return view('edit');
    }
    //保存权限修改
    public function permissionSave(){
        $permission_ids = input('post.')['permission_id'];
        $role_id = input('post.role_id');
        $role_name = input('post.name');
        $role = db('role')->where("role_id = '$role_id'")->find();
        if($role_name != $role['name']){
            $data = ['name'=>$role_name];
            $validate = validate('Rolename');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $ret = db('role')->where("role_id = $role_id")->setField('name', "$role_name");
        }
       
            $rolePermission = model('RolePermission');
            $rolePermission->deletePermission($role_id);
            $rolePermission->addPermission($role_id,$permission_ids);
            $this->success('编辑成功!','Auth/index');

    }

    //删除角色
    public function delete(){
        $role_id = input('get.role_id'); 
        // dump($role_id);die;
        $role = db('role')->where("role_id = '$role_id'")->find();
        $rolePermission = model('RolePermission');
        $rolePermission->deletePermission($role['role_id']);
        $ret = db('role')->where("role_id = '$role_id'")->delete();
        if($ret){
            $this->success('删除成功!','Auth/index');
        }else{
            $this->error('删除失败');
        }
    }
}
